class PromptWindow {
    static #instance = null;

    constructor() {
        if(PromptWindow.#instance){
            return PromptWindow.#instance;
        }
        PromptWindow.#instance = this;
        return PromptWindow.#instance;
    }

    setattr(color, headinfo, info, type = true) {
        this.color = color;
        this.headinfo = headinfo;
        this.info = info;
        this.type = type;
    }
    // 初始化并渲染窗口
    init(callback) {

        let box = this.createEle("div", {}, {
            "width": "400px",
            "height": "200px",
            "background-color": "#fff",
            "box-shadow": "0 10px 30px rgba(0, 0, 0, 0.1)",
            "border-radius": "8px",
            "position": "absolute",
            "top": "50%",
            "left": "50%",
            "transform": "translate(-50%, -50%)",
            "display": "flex",
            "flex-direction": "column",
            "overflow": "hidden"
        }, document.body);

        // Header
        let header = this.createEle("div", {}, {
            "background-color": this.color,
            "padding": "15px 20px",
            "color": "#fff",
            "font-size": "18px",
            "font-weight": "bold",
            "text-align": "left"
        }, box);
        header.textContent = this.headinfo;

        // Content
        let content = this.createEle("div", {}, {
            "padding": "20px",
            "text-align": "center",
            "flex-grow": "1",
            "display": "flex",
            "align-items": "center",
            "justify-content": "center"
        }, box);
        content.textContent = this.info;

        // Bottom (buttons)
        let bottom = this.createEle("div", {}, {
            "display": "flex",
            "justify-content": "space-around",
            "padding": "10px",
            "background-color": "white",
            "border-top": "1px solid #ddd",
            "height": "40px",
        }, box);

        // 确定按钮
        let okBtn = this.createEle("button", {"class": "ok-btn"}, {
            "background-color": "white",
            'font-family':'微软雅黑',
            "border": "none",
            "color": "#5dacf8",
            "padding": "0px 20px",
            "text-align": "center",
            "text-decoration": "none",
            "display": "inline-block",
            "font-size": "18px",
            "margin": "0px 2px",
            "cursor": "pointer",
        }, bottom);
        okBtn.textContent = "确定";

        okBtn.addEventListener('click',e=>{
            document.body.removeChild(box)

            callback(true);

        })



        // 如果有取消按钮，则添加它
        if (!this.type) {
            let fg = this.createEle("div", {}, {
                "background-color": 'rgb(221, 221, 221)',
                'width':'1px',
                "height": "40px",
            }, bottom);
            let cancelBtn = this.createEle("button", {"class": "cancel-btn"}, {
                "background-color": "white",
                'font-family':'微软雅黑',
                "border": "none",
                "color": "#5dacf8",
                "padding": "0px 20px",
                "text-align": "center",
                "text-decoration": "none",
                "display": "inline-block",
                "font-size": "18px",
                "margin": "0px 2px 0px 1px",
                "cursor": "pointer",
            }, bottom);
            cancelBtn.textContent = "取消";
            cancelBtn.addEventListener('click',e=>{
                document.body.removeChild(box);
                callback(false);
            })
        }
        return box;
    }

    // 创建元素的方法
    createEle(nodeName, attributes, styles, parent = null) {
        let target = document.createElement(nodeName);

        // 设置属性
        for (let attr in attributes) {
            if (attributes.hasOwnProperty(attr)) {
                target.setAttribute(attr, attributes[attr]);
            }
        }

        // 设置样式
        for (let style in styles) {
            if (styles.hasOwnProperty(style)) {
                target.style[style] = styles[style];
            }
        }

        // 添加到父节点
        if (parent) {
            parent.appendChild(target);
        }

        return target;
    }

}